// pages/index/index.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let utils = require('../../../utils/util');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    page: 1,
    limit: 10,
    isEnd: false,
    model: false,
    search: '',
    fileId:'',
    isWithout: false,
   
  },
  getData(search,fileId) {
    const that = this;
    let data = {
      q: that.data.search,
      limit: that.data.limit,
      store_id:wx.getStorageSync('select_store_id'),
      p: that.data.page,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.get(api.reqUrls.search, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.goods_list.length > 0) {
          that.setData({
            listData: res.data.result.goods_list
          })
        } else {
          that.setData({
            isWithout: true
          })
        }
      }
    }).catch(err => {
      console.log(err, 888)
    })
  },
  toParticulars(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/goods/pages/goodsDetails/goodsDetails?goodsId=${id}`
    })
  },
  debounces: utils.throttle(function (e) {
    this.addCart(e[0])
  }, 1000),
  // 加入购物车
  addCart(e) {
    const that = this;
    let goodId = e.currentTarget.dataset.goods_id
    let data = {
      goods_num: 1,
      goods_id: goodId,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.addCart, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showModal({
          showCancel: false,
          content: res.data.message,
          success(res) {
            if (res.confirm) {

            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({
      search: options.search||'',
      fileId:options.fileId || ''
    })
    that.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      page: 1,
      isWithout: false,
      isEnd: false,
    })
    wx.showNavigationBarLoading()
    that.getData(that.data.search);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    if (!that.data.isEnd) {
      wx.showLoading({
        title: '加载中...',
      })
      let page = that.data.page;
      page++;
      let data = {
        q: that.data.search,
        limit: that.data.limit,
        store_id:wx.getStorageSync('select_store_id'),
        p: page,
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.get(api.reqUrls.search, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          if (res.data.result.goods_list.length == 0) {
            that.setData({
              isEnd: true
            })
          } else {
            let listData = [...that.data.listData, ...res.data.result.goods_list]
            that.setData({
              page,
              listData
            })
          }
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})